import 'package:flutter/material.dart';
import 'package:xiongan_app_yy/common/user_singleton.dart';
import 'package:xiongan_app_yy/entrys/rp/inv_list_entity.dart';
import 'package:xiongan_app_yy/net/internet_api.dart';
import 'package:xiongan_app_yy/pages/common/LoadBuilder.dart';
import 'package:xiongan_app_yy/widgets/comm/label_select.dart';
import 'package:xiongan_app_yy/widgets/comm/left_icon_widget.dart';
import 'package:xiongan_app_yy/widgets/list_widget.dart';

//配送预约列表
class DeliveryListPage extends StatefulWidget {
  final Object args;

  @override
  State<StatefulWidget> createState() {
    return DeliveryListPageState();
  }

  DeliveryListPage(this.args);
}

class DeliveryListPageState extends State<DeliveryListPage> {
  InvListEntity invListEntity;
  List<String> _labelText = ['未审核', '审核通过', "审核未通过"];
  int selected = -1;
  List<InvListData> unauditedList = new List();
  List<InvListData> auditPassList = new List();
  List<InvListData> auditFailedList = new List();
  List<InvListData> exhibitionList = new List();
  bool load = true;

  @override
  void initState() {
    getData();
  }

  // 迭代器生成list
  Iterable<Widget> get _inputSelects sync* {
    for (int i = 0; i < _labelText.length; i++) {
      yield InputSelect(
        index: i,
        choice: _labelText[i],
        parent: this,
      );
    }
  }

  onSelectedChanged(int index) {
    setState(() {
      selected = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        endDrawer: Drawer(
          child: Container(
            padding: EdgeInsets.only(left: 10, right: 10),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Container(
                    margin: EdgeInsets.only(top: 80, bottom: 10),
                    child: Text("审核状态",
                        style:
                            TextStyle(fontSize: 14, color: Color(0xFF646464)))),
                Expanded(
                  child: Wrap(
                    children: _inputSelects.toList(),
                  ),
                ),
                Container(
                    margin: EdgeInsets.only(bottom: 20),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.end,
                      children: <Widget>[
                        FlatButton(
                            onPressed: () {
                              setState(() {
                                exhibitionList = invListEntity.datas;
                              });
                              Navigator.pop(context);
                            },
                            child: Text(
                              "重置",
                              style: TextStyle(color: Color(0xFF00A5F1)),
                            ),
                            color: Color(0xFFE9E9E9),
                            shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.only(
                                    bottomLeft: Radius.circular(20),
                                    topLeft: Radius.circular(20)))),
                        FlatButton(
                            onPressed: () {
                              print(selected);
                              if (selected == 0) {
                                setState(() {
                                  exhibitionList = unauditedList;
                                });
                              } else if (selected == 1) {
                                setState(() {
                                  exhibitionList = auditPassList;
                                });
                              } else if (selected == 2) {
                                setState(() {
                                  exhibitionList = auditFailedList;
                                });
                              }
                              Navigator.pop(context);
                            },
                            child: Text(
                              "确定",
                              style: TextStyle(color: Colors.white),
                            ),
                            color: Color(0xFF00A5F1),
                            shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.only(
                                    bottomRight: Radius.circular(20),
                                    topRight: Radius.circular(20))))
                      ],
                    ))
              ],
            ),
          ),
        ),
        appBar: new AppBar(
          leading: LeftIconWidget(icon: Icons.chevron_left, isize: 36),
          backgroundColor: Color(0XFF00A5F1),
          title: Text(
            "配送预约",
            style: TextStyle(fontSize: 18),
          ),
          actions: <Widget>[
            new IconButton(
              // action button
              padding: EdgeInsets.only(),
              iconSize: 22,
              icon: new Icon(IconData(0xe617, fontFamily: "wuIconFont")),
              onPressed: () {
                print("开始搜索~~~~~~~~~");
                Navigator.pushNamed(context, "/delivery_search_page");
              },
            ),
            Builder(
              builder: (context) => new IconButton(
                // action button
                iconSize: 22,
                icon: new Icon(IconData(0xe621, fontFamily: "wuIconFont")),
                onPressed: () {
                  Scaffold.of(context).openEndDrawer();
                },
              ),
            ),
          ],
          elevation: 0,
          centerTitle: true,
          //centerTitle: true,
        ),
        body: LoadBuilder(
          visibility: load,
          emptyVisibility: !load && exhibitionList.length == 0,
          emptyTextVisibility: !load && exhibitionList.length == 0,
          textEmptyTips: "",
          builder: new Column(
            children: <Widget>[
              Expanded(
                  child: RefreshIndicator(
                child: ListView.separated(
                    itemBuilder: (BuildContext context, int index) {
                      InvListData data = exhibitionList.elementAt(index);
                      return ListWidget(
                          context: context,
                          index: index,
                          data: data,
                          type: "配送预约单",
                          back: (value) {
                            if (value == "ok") {
                              getData();
                            }
                          });
                    },
                    separatorBuilder: (BuildContext context, int index) =>
                        Divider(height: 1, color: Colors.transparent),
                    itemCount: exhibitionList.length),
                onRefresh: _refresh,
              )),
              Container(
                width: double.infinity,
                height: 48,
                child: FlatButton(
                  color: Color(0xFF00A5F1),
                  child: Text(
                    "新建预约",
                    style: TextStyle(color: Colors.white),
                  ),
                  onPressed: () {
                    Navigator.pushNamed(context, "/delivery_list_page_new")
                        .then((value) {
                      if (value == "ok") {
                        print("ok");
                        getData();
                      }
                    });
                  },
                ),
              )
            ],
          ),
        ));
  }

  Future<void> _refresh() async {
    await Future.delayed(Duration(seconds: 1), () {
      return null;
    });
    setState(() {
      load = true;
    });
    getData();
  }

  void getData() {
    unauditedList.clear();
    auditPassList.clear();
    auditFailedList.clear();
    exhibitionList.clear();
    InternetAPI.getInstance()
        .get("OMS/MiWarehouseOutTask/GetAllMiWarehouseOutTask", (data) {
      invListEntity = InvListEntity.fromJson(data);
      print("1111");
      for (int i = 0; i < invListEntity.datas.length; i++) {
        if (invListEntity.datas.elementAt(i).state == "未分配") {
          unauditedList.add(invListEntity.datas[i]);
        } else if (invListEntity.datas.elementAt(i).state == "未审批") {
          unauditedList.add(invListEntity.datas[i]);
        } else if (invListEntity.datas.elementAt(i).state == "未入库") {
          auditPassList.add(invListEntity.datas[i]);
        } else if (invListEntity.datas.elementAt(i).state == "已入库") {
          auditPassList.add(invListEntity.datas[i]);
        } else if (invListEntity.datas.elementAt(i).state == "已驳回") {
          auditFailedList.add(invListEntity.datas[i]);
        }
      }

      setState(() {
        load = false;
        if (widget.args != null) {
          switch (widget.args) {
            case 0:
              exhibitionList = unauditedList;
              selected = 0;
              break;
            case 1:
              exhibitionList = auditPassList;
              selected = 1;
              break;
            case 2:
              exhibitionList = auditFailedList;
              selected = 2;
              break;
            default:
              exhibitionList = invListEntity.datas;
              selected = -1;
              break;
          }
        } else {
          exhibitionList = invListEntity.datas;
        }
      });
    }, params: {
      'operaterUserId': DataSingleton().Data.id,
      'tenantId': DataSingleton().Data.tenantId,
      "Warehouseouttype": "20"
    });
  }
}
